/*
 *	OpenSource code or fragments by avocado systems, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.util;

import java.io.IOException;

import javax.swing.SwingUtilities;

/**
 * Implements a file executing runnable.
 * Invokes a new thread to run an OS related command to execute a file.
 * 
 *	@date 20.11.2008
 *  @author Marcus
 */
public class BrowserLauncher implements Runnable {

	private static BrowserLauncher self;
	private String exec;
	private final String args = "";

	/**
	 * Singleton pattern.
	 * 
	 * @return this
	 */
	public static synchronized BrowserLauncher getInstance() {
		if (self == null) {
			BrowserLauncher.self = new BrowserLauncher();
			return BrowserLauncher.self;
		}
		return BrowserLauncher.self;
	}

	/**
	 * Sets the executable and invokes a new thread.
	 * 
	 * @param executable the executable
	 */
	public void execute(final String executable) {
		if (executable == null) {
			//throw new NullPointerException();
		}
		if (executable.equalsIgnoreCase("")) {
			//throw new NullPointerException();
		}
		exec = "\"" + executable + "\"";
		
		SwingUtilities.invokeLater(new Thread(this));
	}

	/**
	 * The run-method of this Runnable.
	 * Evaluates the OS and calls the runtime to execute the command line arguments.
	 */
	public void run() {
		try {
			if (OSValidator.isWindows()) {
				if (exec.endsWith(".jar\"")) {
					Runtime.getRuntime().exec("cmd /c java -jar " + exec);
				} else if (!exec.endsWith(".exe\"")) {
					Runtime.getRuntime().exec("cmd /c " + exec);
				} else {
					new ProcessBuilder(exec, args).start();
				}
			} else if (OSValidator.isUnix()) {
				final String[] cmd = { "/bin/sh", "-c", exec };
				Runtime.getRuntime().exec(cmd);
			} else if (OSValidator.isMac()) {
				final String[] cmd = { "open ", exec };
				Runtime.getRuntime().exec(cmd);
			}
		} catch (final IOException e) {
			Tools.logManager.finest(e.getMessage());
		}
	}
}